var path = require('path');
var Webpack = require('webpack');
var PurifyCss = require('purifycss-webpack');
var glob = require('glob-all');
var ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
// const extractLess = new ExtractTextWebpackPlugin('stylesheets/[name]-two.css');
var extractLess = new ExtractTextWebpackPlugin({
    filename: '[name].bundle-[hash:5].css'
})
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlInlinkChunPlugin = require('html-webpack-inline-chunk-plugin');
var CleanWebpackPlugin = require('clean-webpack-plugin');
var devServer = require('webpack-dev-server')

module.exports = {
    entry: {
        app: './src/app.js'
    },
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/',
        filename: 'js/[name]-bundle-[hash:5].js'
        // chunkFilename: '[name].bundle.js'
    },
    devtool: 'cheap-module-source-map',
    devServer: {
        port: 9001,
        overlay: true,
        proxy: {

        },
        hot: true,
        hotOnly: true,
        historyApiFallback: {
            rewrites: [{
                from: /^\ ([a - zA - Z0 - 9] + \/?)([a-zA-Z0-9]+)/,
                to: function (content) {
                    return '/' + context / match[1] + context.match[2] + '.html'
                }
            }]
        }
    },
    module: {
        rules: [{
                test: /\.js$/,
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ['env']
                    }
                }]
            },
            {
                test: /\.less$/,
                use: [{
                        loader: 'style-loader',
                        options: {
                            // singleton: true,
                            // transform: './css.tranform.js'
                            sourceMap: true,

                        },
                    }, {
                        loader: 'css-loader',
                        options: {
                            importLoaders: 2,
                            sourceMap: true,
                            // minimize: true,
                            // modules: true,
                            // localIdentName: '[path][name]_[local]_[hash:base64:5]'
                        }
                    },
                    {
                        loader: 'postcss-loader',
                        options: {
                            ident: 'postcss',
                            sourceMap: true,
                            plugins: [
                                // require('autoprefixer')(),
                                require('postcss-sprites')({
                                    spritePath: 'dist/accets/imgs/',
                                    retina: true
                                }),
                                require('postcss-cssnext')()
                            ]
                        }
                    },
                    {
                        loader: 'less-loader',
                        options: {
                            sourceMap: true,
                        }
                    }
                ]
            },
            // {
            //     test: /\.less$/,
            //     use: extractLess.extract({
            //         fallback: {
            //             loader: 'style-loader',
            //             options: {
            //                 singleton: true,
            //                 // transform: './css.tranform.js'
            //             }
            //         },
            //         use: [{
            //                 loader: 'css-loader',
            //                 options: {
            //                     importLoaders: 2
            //                     // minimize: true,
            //                     // modules: true,
            //                     // localIdentName: '[path][name]_[local]_[hash:base64:5]'
            //                 }
            //             },
            //             {
            //                 loader: 'postcss-loader',
            //                 options: {
            //                     ident: 'postcss',
            //                     plugins: [
            //                         // require('autoprefixer')(),
            //                         require('postcss-sprites')({
            //                             spritePath: 'dist/accets/imgs/',
            //                             retina: true
            //                         }),
            //                         require('postcss-cssnext')()
            //                     ]
            //                 }
            //             },
            //             {
            //                 loader: 'less-loader'
            //             }
            //         ]
            //     }),
            // },
            {
                test: /\.js$/,
                include: [path.resolve(__dirname, 'src')],
                exclude: [path.resolve(__dirname, 'src/lib')],
                use: [{
                    loader: 'babel-loader',
                    options: {
                        presets: ['env'],
                        plugins: ['lodash']
                    }
                }, {
                    loader: 'eslint-loader',
                    options: {
                        formatter: require('eslint-friendly-formatter'),
                    }
                }]
            },
            {
                test: /\.(png|jpg|gif)$/,
                use: [{
                    loader: 'url-loader',
                    options: {
                        name: '[name].[hash:5].[ext]',
                        limit: 1000,
                        outputPath: 'accets/imgs/',
                        // useRelativePath: true,
                    }
                }, {
                    loader: 'img-loader',
                    options: {
                        pngquant: {

                        }
                    }
                }]
            },
            // {
            //     test:/\.html$/,
            //     use:[
            //         {
            //             loader:'html-loader',
            //             options:{
            //                 attrs:['img:src']//,'img:data-src']
            //             }
            //         }
            //     ]
            // }
        ]
    },
    plugins: [
        extractLess,
        new PurifyCss({
            paths: glob.sync([
                path.join(__dirname, './*.html'),
                path.join(__dirname, './src/*.js'),
            ])
        }),
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: './index.html',
            // chunks:['app'],
            minify: {
                collapseWhitespace: true
            }
            // inject:false,
        }),
        new Webpack.ProvidePlugin({
            $: 'jquery'
        }),
        new Webpack.optimize.CommonsChunkPlugin({
            name: 'manifest',
        }),
        new HtmlInlinkChunPlugin({
            inlineChunks: ['manifest']
        }),
        // new Webpack.optimize.UglifyJsPlugin(),
        new CleanWebpackPlugin(
            ['dist']
        ),
        new Webpack.HotModuleReplacementPlugin(),
        new Webpack.NamedModulesPlugin(),
    ]
}
